﻿using System;
using System.Collections.Generic;
using DBUtility;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data.SqlClient;
using System.Data.SqlTypes;

namespace IYAKASAPortal.Statistics {
    public partial class PrintAJ_Driver : System.Web.UI.Page {
        private static string s_strStart;
        private static string s_strEnd;
        protected void Page_Load(object sender, EventArgs e) {
            btnSearch_Click(sender, e);
        }

        protected void btnSearch_Click(object sender, EventArgs e) {
            var _webFront = this;
            s_strStart = _webFront.txtStart.Text;
            s_strEnd = _webFront.txtEnd.Text;
            DataTable dt_TempSet = new DataTable();
            StringBuilder sbSql = new StringBuilder();

            if (string.IsNullOrEmpty(s_strStart) && string.IsNullOrEmpty(s_strEnd)) {
                s_strStart = "2000-01-01 00:00:00";
                s_strEnd = string.Format("{0} 23:59:59", DateTime.Now.ToString("yyyy-MM-dd"));
                _webFront.txtStart.Text = s_strStart;
                _webFront.txtEnd.Text = s_strEnd;
            }
            else {
                s_strStart = s_strStart.Substring(0, 11);
                s_strEnd = s_strEnd.Substring(0, 11);
                s_strStart += "00:00:00";
                s_strEnd += "23:59:59";
            }
            DBUtility.DbStoredProcedure _sqlSpExecute = new DbStoredProcedure();
            var _sqlParam1 = _sqlSpExecute.MakeInParam("StartTime", SqlDbType.DateTime, s_strStart.Length, s_strStart);
            var _sqlParam2 = _sqlSpExecute.MakeInParam("EndTime", SqlDbType.DateTime, s_strEnd.Length, s_strEnd);
            SqlParameter[] _paramList = new SqlParameter[2];
            _paramList[0] = _sqlParam1;
            _paramList[1] = _sqlParam2;
            _sqlSpExecute.GetProcDataTable(ref dt_TempSet, "SP_REPORT_AJ_DRIVER_STAT", _paramList);
            this.GetCount(dt_TempSet);
            this.rptList.DataSource = dt_TempSet;
            this.rptList.DataBind();
            dt_TempSet.Dispose();
            //UPDATE PAGE INFO
            _webFront.txtStart.Text = s_strStart;
            _webFront.txtEnd.Text = s_strEnd;
            _webFront.lblDate.Text = s_strStart + "~" + s_strEnd;
        }
        private void GetCount(DataTable dtDataSet) {
            var _webFront = this;
            int _iCLZS = 0;
            int _iHGS = 0;
            int _iFJHG = 0;
            int _iSCHG = 0;
            int _iDCHG = 0;
            foreach (DataRow row in dtDataSet.Rows) {
                _iCLZS += Convert.ToInt32(row["CLZS"]);
                _iHGS += Convert.ToInt32(row["HGS"]);
                _iFJHG += Convert.ToInt32(row["FJHG"]);
                _iSCHG += Convert.ToInt32(row["SCHG"]);
                _iDCHG += Convert.ToInt32(row["DCHG"]);
            }
            _webFront.lblCJS.Text = _iCLZS.ToString();
            _webFront.lblHGS.Text = _iHGS.ToString();
            _webFront.lblYHGS.Text = _iFJHG.ToString();
            _webFront.lblEHGS.Text = _iSCHG.ToString();
            _webFront.lblSHGS.Text = _iDCHG.ToString();

            if (_iCLZS > 0) {
                double _fHGL = Math.Round((_iHGS * 1.0 / _iCLZS), 2);
                double _fYHGL = Math.Round((_iFJHG * 1.0 / _iCLZS), 2);
                double _fEHGL = Math.Round((_iSCHG * 1.0 / _iCLZS), 2);
                double _fSHGL = Math.Round((_iDCHG * 1.0 / _iCLZS), 2);
                _webFront.lblHGL.Text = (_fHGL*100).ToString() + "%";
                _webFront.lblYHGL.Text = (_fYHGL*100).ToString() + "%";
                _webFront.lblEHGL.Text = (_fEHGL*100).ToString() + "%";
                _webFront.lblSHGL.Text = (_fSHGL*100).ToString() + "%";
            }
            else {
                _webFront.lblHGL.Text = "0%";
                _webFront.lblYHGL.Text = "0%";
                _webFront.lblEHGL.Text = "0%";
                _webFront.lblSHGL.Text = "0%";
            }
        }

        protected void btnSearchAll_Click(object sender, EventArgs e) {
            this.txtStart.Text = string.Empty;
            this.txtEnd.Text = string.Empty;
            btnSearch_Click(sender, e);
        }
    }
}